from django.http import HttpResponse
from django.shortcuts import render_to_response
from forms import DocumentUploadForm
from django.core.context_processors import csrf

from pdf_parser import parse_document
from extractor.extractor import process_document

def index(request):
    c = {}
    c.update(csrf(request))
    if request.method == "GET":
        c["form"] = DocumentUploadForm()
        return render_to_response("index.html",c)
    else:
        f = DocumentUploadForm(request.POST, request.FILES)
        if f.is_valid():
            d = f.cleaned_data["document"]
            document = parse_document(d.read())
            pages = process_document(document)
            return render_to_response("document.html",{"pages" : pages}) 
        else : return HttpResponse("invalid data")